﻿  /****************************************************************************/
 /*   vlaCalendar Clientside v1.0.1 extension for vlaCalendar version 2.1    */
/****************************************************************************/

Date.implement({
	getWeek: function() {
		var oneJan = new Date(this.getFullYear(), 0, 1);
		return Math.ceil((((this - oneJan) / 86400000) + oneJan.getDay()) / 7);
	},
	
	getTs: function() {
		return parseInt((this.getTime() / 1000));
	},
	
	getMonthNo: function() {
		return (this.getMonth() + 1);
	}
});

vlaCalendar.implement({
	//New vars
	'weekDayLabels': ['一', '二', '三', '四', '五', '六', '日'],
	'monthLabels': ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
	'monthSmallLabels': ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
	
	//Replaced functions
	u: function(_url, _pars, _onComplete, _id) {
		if(!this.loading && !this.transitioning) {
			this.loading = true;
			var _class = this;
			var element = $(_id ? _id : this.tempLoader);
			_pars += '&defaultView='+  _url;
			_pars += '&picker='+ (this.picker ? 1 : 0) +'&startMonday='+ (this.startMonday ? 1 : 0) +'&style='+ this.style;
			if(this.picker && this.getInputDate()) _pars += '&pickedDate='+ this.getInputDate();
			element.set('html', this.getHTML(_pars));
			_onComplete();
			this.loading = false;
		}
	},
	
	//New functions
	getHTML: function(args) {
		var pars = {};
		vars = args.split('&').each(function (e) {
			param = e.split('=');
			if(!pars[param[0]]) pars[param[0]] = param[1];
		});
		
		pars.startMonday = pars.startMonday.toInt();
		pars.picker = pars.picker.toInt();
		if(pars.gotoPickedDate) pars.gotoPickedDate = pars.gotoPickedDate.toInt();
		
		var pickedDate = null;
		if($defined(pars.pickedDate)) {
			if(pars.pickedDate == 't') {
				pickedDate = this.mkdate();					
				this.setDate(pickedDate);
			} else {
				match = pars.pickedDate.split('/');
				
				if(match != null) {
					if(match.length > 0) {
						pickedDate = this.mkdate(match[2], match[1] - 1, match[0]);
						if(pickedDate.getMonthNo() != match[1]) {
							pickedDate = this.mkdate(match[2], match[1] - 1, this.daysInMonth(match[2], match[1] - 1));
							this.setDate(pickedDate);
						}
					}
				}
			}
		}
		if(pickedDate == null) pickedDate = this.mkdate();
			
		if($defined(pars.ts)) {
			var date = new Date();
			date.setTime(parseInt(pars.ts) * 1000);
			ts = date.getTs();
		} else {
			ts = this.mktime(pickedDate.getFullYear(), pickedDate.getMonth(), 1);
		}
		
		pars.ts = ts;
		pars['pickedDate'] = pickedDate;
		
		if(pars.defaultView == 'decade') var picker = this.getDecadeHTML(pars);
		else if(pars.defaultView == 'year') var picker = this.getYearHTML(pars);
		else var picker = this.getMonthHTML(pars);
		
		if(!$defined(this.createContainer)) {
			this.createContainer = false;
			div  = '<div class="vlaCalendar'+ ($defined(args.style) ? ' '+ args.style : '') +'">';
				div += '<span class="indication"><div class="arrowRight"></div><div class="arrowLeft"></div>';
					div += '<span class="label" date="';
						div += "{'day': '"+ pickedDate.getDate() +"', 'month': '"+ pickedDate.getMonthNo() +"'";
						div += ", 'year': '"+ pickedDate.getFullYear() +'\'}">&nbsp;';
					div += '</span>';
				div += '</span>';
				div += '<div class="container">';
					div += '<div class="loaderB"></div>';
					div += '<div class="loaderA">'+ picker +'</div>';
				div += '</div>';
			div += '</div>';
			return div;
		} else return picker;
	},

	getMonthHTML: function(args) {
		var ts = this.ts2date(args.ts);
		var ts_year = ts.getFullYear();
		var ts_month = ts.getMonth();
		var ts_month_name = this.monthLabels[ts_month];
		var ts_nrodays = this.daysInMonth(ts.getFullYear(), ts.getMonth());
		
		var pr_date = this.mkdate(ts_year, ts_month-1, 1);
		var nx_date = this.mkdate(ts_year, ts_month+1, 1);
		
		var wdays_counter = ts.getDay() - (args.startMonday ? 1 : 0);
		if(wdays_counter == -1) wdays_counter = 6;
		
		ts = ts.getTs();
		var t = '<table class="month'+ (args.picker ? ' picker' : '') +'" cellpadding="0" summary="{';
			t += "'ts': '"+ ts +"', 'pr_ts': '"+ pr_date.getTs() +"', 'nx_ts': '"+ nx_date.getTs() +"', 'label': '"+ ts_month_name +", "+ ts_year +"'";
			t += ", 'current': 'month', 'parent': 'year'";
		t += '}">';
		t += '<tr>';
		
		if(args.startMonday) {
			var last_day = 6;
		} else {
			t += '<th>'+ this.weekDayLabels[6] +'</th>';
			var last_day = 5;
		}
		for(i = 0; i <= last_day; i++) {
			t += '<th>'+ this.weekDayLabels[i] +'</th>';
		}
		t += '</tr>';
		t += '<tr class="firstRow">';
		
		var row = 0;
		
		//Add days for the beginning non-month days
		for(i = 0; i < wdays_counter; i++) {
			last_day = this.daysInMonth(pr_date.getFullYear(), pr_date.getMonth());
			var day = last_day - (wdays_counter-i) + 1;
			i_date = this.mkdate(ts_year, ts_month-1, day, ts_year);
			t += '<td class="outsideDay" date="'+"{";
			t += "'day': '"+ day +"', 'month': '"+ i_date.getMonthNo() +"', 'year': '"+ i_date.getFullYear() +"'}";
			t += '">'+ day +'</td>';
		}
		
		//Add month days
		var pd_ts = args.pickedDate.getTs();
		for(i = 1; i <= ts_nrodays; i++) {
			i_date = this.mkdate(ts_year, ts_month, i); 
			i_ts = i_date.getTs();
			t += '<td'+ (i_ts == pd_ts ? ' class="selected"' : '');
			t += ' '+"date=\"{'day': '"+ i +"', 'month': '"+ i_date.getMonthNo() +"', 'year': '"+ ts_year +"'}\">";
			t += i; 
			t += '</td>';
			
			if(wdays_counter == 6) {
				// && (i - 1) != ts_nrodays) {
				week_num = i_date.getWeek() + 1;
				t += "</tr><tr>";
				wdays_counter = -1;
				row++;
			}
			wdays_counter++;
		}
		
		//Add outside days
		var a = 1;
		if(wdays_counter !== 0) {
			for(i = wdays_counter; i < 7; i++) {
				i_date = this.mkdate(ts_year, ts_month + 1, a); 
				i_ts = i_date.getTs();
				t += '<td class="outsideDay" date="'+"{'day': '"+ a +"', 'month': '"+ i_date.getMonthNo() +"', 'year': '"+ i_date.getFullYear() +"'}"+'">'+ a +'</td>';
				a++;
			}
			row++;
		}
		
		//Always have 6 rows
		if(row == 4 || row == 5) {
			if(wdays_counter !== 0) { t += "</tr><tr>"; }
			for(i = 0; i < (row == 5 ? 7 : 14); i++) {
				i_date = this.mkdate(ts_year, ts_month + 1, a); 
				t += '<td class="outsideDay" date="'+"{'day': '"+ a +"', 'month': '"+ i_date.getMonthNo() +"', 'year': '"+ i_date.getFullYear() +"'}"+'">'+ a +'</td>';
				a++;
				if(i == 6) { t += "</tr><tr>"; }
			}
		}	
		
		t += '</tr>';
		t += '</table>';
		return t;
	},

	getYearHTML: function(args) {
		var ts = this.ts2date(args.ts);
		if(args.parent == 'month') var m_ts = this.mktime(ts.getFullYear(), ts.getMonth(), 1); //Selected month timestamp
		else var m_ts = '';
		
		var pickedDate = args.pickedDate;
		
		var ts_year	= ts.getFullYear();
		var pr_ts = this.mktime(ts_year-1, 0, 1);
		var nx_ts = this.mktime(ts_year+1, 0, 1);
		
		ts = ts.getTs();
		var t = '<table class="year" cellpadding="0" summary="';
		t += "{'ts': '"+ ts +"', 'pr_ts': '"+ pr_ts +"', 'nx_ts': '"+ nx_ts +"', 'label': '";
			t += ts_year +"', 'current': 'year', 'parent': 'decade'";
		t += '}">';
		
		//Add years
		var m = 0;
		for(i = 0; i < 3; i++) {
			t += "<tr>";
			for(y = 0; y < 4; y++) {
				i_date = this.mkdate(ts_year, m, 1);
				i_ts = i_date.getTs();
				var current = (pickedDate.getMonthNo() == i_date.getMonthNo() && pickedDate.getFullYear() == i_date.getFullYear());
				t += '<td ts="'+ i_ts +'" class="'+ (m_ts == i_ts ? 'selected' : '') + (current ? 'current' : '') +'">'+ this.monthSmallLabels[i_date.getMonth()] +'</td>';
				m++;
			}
			t += "</tr>";
		}
		t += '</table>';
		return t;
	},

	getDecadeHTML: function(args) {
		var ts = this.ts2date(args.ts);
		var ts_year	= parseInt(ts.getFullYear());
		var decade = [ts_year-5, ts_year+5];
		
		var y_ts = this.mktime(ts.getFullYear(), 0, 1); 
		var m_ts = args.m_ts;
		
		var pr_ts = this.mktime(ts_year - 12, 0, 1);
		var nx_ts = this.mktime(ts_year + 12, 0, 1);
		
		var t = '<table class="year" cellpadding="0" summary="{';
		t += "'ts': '"+ ts.getTs() +"', 'pr_ts': '"+ pr_ts +"', 'nx_ts': '"+ nx_ts +"', 'label': '";
		t += (decade[0]) +' - '+ (decade[1]+1) +"', 'current': 'decade'"; 
		t += '}">';
		
		//Add decades
		var year = decade[0];
		for(i = 0; i < 3; i++) {
			t += "<tr>";
			for(y = 0; y < 4; y++) {
				i_ts = this.mktime(year, 0, 1);
				i_date = this.mkdate(year, 0, 1);
				t += '<td ts="'+ i_ts +'" m_ts="'+ m_ts +'" class="'+ (args.parent && y_ts == i_ts ? 'selected' : '') + (args.pickedDate.getFullYear() == i_date.getFullYear() ? 'current' : '') +'">'+ year +'</td>';
				year++;
			}
			t += "</tr>";
		}
		t += '</table>';
		
		return t;
	},
	
	setDate: function(jsdate) {
		this.pick({day: jsdate.getDate(), month: jsdate.getMonthNo(), year: jsdate.getFullYear()});
	},
	
	daysInMonth: function(iYear, iMonth) {
		return 32 - new Date(iYear, iMonth, 32).getDate();
	},

	mkdate: function(year, month, day) {
		var date = new Date();
		if($defined(year)) date.setYear(year);
		if($defined(month)) date.setMonth(month);
		if($defined(day)) date.setDate(day);
		
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		date.setMilliseconds(0);
		return date;
	},

	ts2date: function(ts) {
		var date = new Date();
		date.setTime(parseInt(ts) * 1000);
		return date;
	},

	mktime: function(year, month, day) {
		var date = this.mkdate(year, month, day);
		return date.getTs();
	}
}); 